#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdbool.h>
#include <signal.h>

#define ERROR(emg) do{\
                        fprintf(stderr,"file:%s,func:%s,line:%d\n",__FILE__,__func__,__LINE__);\
                        perror(emg);\
                        exit(-1);\
                    }while(0);

void handle_signal(int sig)
{
    fprintf(stdout,"回收子进程资源\n");
    wait(NULL);//waitpid(-1,NULL,0);
}

int main(int argc,char const* argv)
{
    pid_t pid=fork();
    if(pid<0){
       ERROR("fork error");
    }else if(pid>0){
        signal(SIGCHLD,handle_signal);
        while(1){
            printf("father is runing\n");
            sleep(1);
        }
    }else{
       printf("子进程正在运行\n");
       sleep(2);
#if 1
        system("clear");
        system("ls -al");
       //execlp("ls","ls","-al",NULL);
       //execl("/bin/ls","ls","-al",NULL);
#else
       char *cmd[]={"ls","-al",NULL};
       execv("/bin/ls",cmd);//execvp("ls",cmd);
#endif
       printf("hello world\n");
    }
    return 0;
}